package com.hjf.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.commons.po.DeptPo;
import com.commons.po.PageBean;
import com.dao.BaseDao;
import com.hjf.dao.DeptPoDao;


public class DeptPoDaoImpl extends BaseDao<DeptPo> implements DeptPoDao {

	@Override
	public List<DeptPo> findDeptAll() {
		String sql="select * from dept";
		return this.executeQuery(sql, null, DeptPo.class, null);
	}

	@Override
	public List<DeptPo> findDeptById(Integer id) {
		String sql="select * from dept where dept_id=?";
		Object[] paramsObjs={id};
		return this.executeQuery(sql, null, DeptPo.class, paramsObjs);
	}

	@Override
	public int saveDeptPo(DeptPo entity) {
		String sql="insert into dept values(seq_dept_id.nextval,?,?,?,?,?,?,?,?)";
		List<Object> paramsObjs=new ArrayList<Object>();
		paramsObjs.add(entity.getDeptName());
		paramsObjs.add(entity.getDeptParent());
		paramsObjs.add(entity.getCreatedId());
		paramsObjs.add(entity.getCreatedDate());
		paramsObjs.add(entity.getUpdateId());
		paramsObjs.add(entity.getUpdateDate());
		paramsObjs.add(entity.getIsActive());
		paramsObjs.add(entity.getDeptIntroduce());
		return this.executeUpdate(sql, paramsObjs.toArray());
	}

	@Override
	public int updateDeptPo(DeptPo entity) {
		String sql="update dept set dept_name=?,dept_parent=?,created_id=?,created_date=?,update_id=?,update_date=?,is_active=?,dept_introduce=? where dept_id=?";
		List<Object> paramsObjs=new ArrayList<Object>();
		paramsObjs.add(entity.getDeptName());
		paramsObjs.add(entity.getDeptParent());
		paramsObjs.add(entity.getCreatedId());
		paramsObjs.add(entity.getCreatedDate());
		paramsObjs.add(entity.getUpdateId());
		paramsObjs.add(entity.getUpdateDate());
		paramsObjs.add(entity.getIsActive());
		paramsObjs.add(entity.getDeptIntroduce());
		paramsObjs.add(entity.getDeptId());
		return this.executeUpdate(sql, paramsObjs.toArray());
	}

	@Override
	public List<DeptPo> findByDeptParent(Integer deptParent) {
		List<Object> paramList=new ArrayList<Object>();
		String sql="";
		if(deptParent!=null){
			sql="select * from dept where dept_parent=?";
			paramList.add(deptParent);
		}else{
			sql="select * from dept where dept_parent is null";
		}
		return this.executeQuery(sql, null, DeptPo.class, paramList.toArray());
	}

	@Override
	public PageBean<DeptPo> findPageByCondition(int pageIndex, int pageSize, Map<String, Object> map) {
		//1、先初始分页对象并设置初始化属性
		PageBean<DeptPo> pagebean=new PageBean<DeptPo>();
		pagebean.setPageIndex(pageIndex);
		pagebean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		
		//2、拼接sql
		StringBuffer baseSql=new StringBuffer();
		baseSql.append(" select * from dept where 1=1 ");
		Integer parent=null;
		if(map!=null&&map.size()>0){
			baseSql.append(" and dept_parent=? ");
			parent=(Integer)map.get("deptId");
			paramList.add(map.get("deptId"));
		}else{
			baseSql.append(" and dept_parent is null ");
		}
		
		//3、获得总记录
		pagebean.setTotalCount(this.findByDeptParent(parent).size()); //pagebean.setTotalCount(this.getCount(baseSql.toString(), paramList.toArray()));
		
		//4、获得分页数据
		if(pagebean.getTotalCount()>0){
			StringBuffer pageSql=new StringBuffer();
			pageSql.append(" select * from ( ");
			pageSql.append("  select tmp1.*,rownum rn from ( ");
			pageSql.append(baseSql.toString());
			pageSql.append("  ) tmp1");
			pageSql.append(" ) tmp2");
			pageSql.append(" where tmp2.rn>? and tmp2.rn<=? ");
			int startRow=(pagebean.getPageIndex()-1)*pagebean.getPageSize();
			int endRow=pagebean.getPageIndex()*pagebean.getPageSize();
			paramList.add(startRow);//开始行号
			paramList.add(endRow);//结束行号
			
//			Map<String, String> columnsMap=new HashMap<String, String>();
//			columnsMap.put("menu_id", "id");//列名与属性名不相同时
			pagebean.setList(this.executeQuery(pageSql.toString(), null, DeptPo.class, paramList.toArray()));
		}
		return pagebean;
	}

}
